﻿Imports System.Reflection
Imports Microsoft.Office.Interop

Public Class Form1

    Private Sub tmrMailCheck_Tick(sender As System.Object, e As System.EventArgs) Handles tmrMailCheck.Tick
        ' Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()
        Dim SAPI
        SAPI = CreateObject("SAPI.spvoice")

        ' Get MAPI NameSpace.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get Messages collection of Inbox.
        'Dim oInbox As Outlook.MAPIFolder


        'Console.WriteLine("Total : " & oItems.Count)

        '**MG**Create a list of all folders
        Dim oFolder As Outlook.MAPIFolder = oNS.Folders("Joel.Ashman@unt.edu")
        Dim oItems As Outlook.MailItem = oFolder.Folders.Items
        'Dim oMsg As Outlook.MailItem
        'Get the top folders and flip through them
        'For Each oFolders In oNS.Folders
        ''If (oFolders.Name = "Inbox" Or oFolders.Name = "Calls") Then
        'For Each MyItem In oFolders.Items
        '    'And find the "Unread" items
        '    If (MyItem.Restrict("[Unread] = true")) Then
        '        'Add them to the list
        '        'oItems = MyItem.Add
        '        SAPI.Speak("New mail from " + oItems.SenderName)
        '    End If
        'Next

        'Get the subfolders and flip through them
        For Each oFolders In oFolder.Folders
            MsgBox(oFolders.Name)
            'If the subfolder is an inbox or calls folder 
            If (oFolders.Name = "Inbox" Or oFolders.Name = "Calls") Then
                'Go through the items in the folder
                For Each MyItem In oFolders.Items
                    'And find the "Unread" items
                    If (MyItem.Restrict("[Unread] = true")) Then
                        'Add them to the list
                        'oItems = MyItem.Add
                        SAPI.Speak("New mail from " + oItems.SenderName)
                    End If
                Next
            End If
        Next
        'End If
        'Next
        'Loop each i message.
        'Dim oMsg As Outlook.MailItem
        'Flip through each item in the created list
        'If oItems.Count > 0 Then
        'For Each oMsg In oItems
        'SAPI.Speak("New mail from " + oMsg.SenderName)
        'Console.WriteLine(i)
        'Console.WriteLine(oMsg.SenderName)
        'Console.WriteLine(oMsg.Subject)
        'Next
        'End If

        ' Log off.
        oNS.Logoff()

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        'oMsg = Nothing
        tmrCallCheck.Start()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


    End Sub

End Class
